home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / pygtk-2.0 / pygobject.h
Encoding:
C/C++ Source or Header  |  2006-01-20  |  10.8 KB  |  268 lines

  1. /* -*- Mode: C; c-basic-offset: 4 -*- */
  2. #ifndef _PYGOBJECT_H_
  3. #define _PYGOBJECT_H_
  4.  
  5. #include <Python.h>
  6.  
  7. #include <glib.h>
  8. #include <glib-object.h>
  9.  
  10. G_BEGIN_DECLS
  11.  
  12. /* Work around bugs in PyGILState api fixed in 2.4.0a4 */
  13. #if PY_VERSION_HEX < 0x020400A4
  14. #define PYGIL_API_IS_BUGGY TRUE
  15. #else
  16. #define PYGIL_API_IS_BUGGY FALSE
  17. #endif
  18.  
  19.   /* PyGClosure is a _private_ structure */
  20. typedef struct _PyGClosure PyGClosure;
  21. struct _PyGClosure {
  22.     GClosure closure;
  23.     PyObject *callback;
  24.     PyObject *extra_args; /* tuple of extra args to pass to callback */
  25.     PyObject *swap_data; /* other object for gtk_signal_connect_object */
  26. };
  27.  
  28. typedef struct {
  29.     PyObject_HEAD
  30.     GObject *obj;
  31.     PyObject *inst_dict; /* the instance dictionary -- must be last */
  32.     PyObject *weakreflist; /* list of weak references */
  33.     GSList *closures;
  34. } PyGObject;
  35.  
  36. #define pygobject_get(v) (((PyGObject *)(v))->obj)
  37. #define pygobject_check(v,base) (PyObject_TypeCheck(v,base))
  38.  
  39. typedef struct {
  40.     PyObject_HEAD
  41.     gpointer boxed;
  42.     GType gtype;
  43.     gboolean free_on_dealloc;
  44. } PyGBoxed;
  45.  
  46. #define pyg_boxed_get(v,t)      ((t *)((PyGBoxed *)(v))->boxed)
  47. #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
  48.  
  49. typedef struct {
  50.     PyObject_HEAD
  51.     gpointer pointer;
  52.     GType gtype;
  53. } PyGPointer;
  54.  
  55. #define pyg_pointer_get(v,t)      ((t *)((PyGPointer *)(v))->pointer)
  56. #define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode)
  57.  
  58. typedef void (*PyGFatalExceptionFunc) (void);
  59. typedef void (*PyGThreadBlockFunc) (void);
  60.  
  61. typedef struct {
  62.     PyObject_HEAD
  63.     GParamSpec *pspec;
  64. } PyGParamSpec;
  65.  
  66. #define PyGParamSpec_Get(v) (((PyGParamSpec *)v)->pspec)
  67. #define PyGParamSpec_Check(v) (PyObject_TypeCheck(v, &PyGParamSpec_Type))
  68.  
  69. typedef int (*PyGClassInitFunc) (gpointer gclass, PyTypeObject *pyclass);
  70.  
  71.  
  72. struct _PyGObject_Functions {
  73.     void (* register_class)(PyObject *dict, const gchar *class_name,
  74.                 GType gtype, PyTypeObject *type, PyObject *bases);
  75.     void (* register_wrapper)(PyObject *self);
  76.     void (* register_sinkfunc)(GType type,
  77.                    void (* sinkfunc)(GObject *object));
  78.     PyTypeObject *(* lookup_class)(GType type);
  79.     PyObject *(* newgobj)(GObject *obj);
  80.  
  81.     GClosure *(* closure_new)(PyObject *callback, PyObject *extra_args,
  82.                   PyObject *swap_data);
  83.     void      (* object_watch_closure)(PyObject *self, GClosure *closure);
  84.     GDestroyNotify destroy_notify;
  85.  
  86.     GType (* type_from_object)(PyObject *obj);
  87.     PyObject *(* type_wrapper_new)(GType type);
  88.  
  89.     gint (* enum_get_value)(GType enum_type, PyObject *obj, gint *val);
  90.     gint (* flags_get_value)(GType flag_type, PyObject *obj, gint *val);
  91.     void (* register_boxed_custom)(GType boxed_type,
  92.                 PyObject *(* from_func)(const GValue *value),
  93.                 int (* to_func)(GValue *value, PyObject *obj));
  94.     int (* value_from_pyobject)(GValue *value, PyObject *obj);
  95.     PyObject *(* value_as_pyobject)(const GValue *value, gboolean copy_boxed);
  96.  
  97.     void (* register_interface)(PyObject *dict, const gchar *class_name,
  98.                 GType gtype, PyTypeObject *type);
  99.  
  100.     PyTypeObject *boxed_type;
  101.     void (* register_boxed)(PyObject *dict, const gchar *class_name,
  102.                 GType boxed_type, PyTypeObject *type);
  103.     PyObject *(* boxed_new)(GType boxed_type, gpointer boxed,
  104.                 gboolean copy_boxed, gboolean own_ref);
  105.  
  106.     PyTypeObject *pointer_type;
  107.     void (* register_pointer)(PyObject *dict, const gchar *class_name,
  108.                   GType pointer_type, PyTypeObject *type);
  109.     PyObject *(* pointer_new)(GType boxed_type, gpointer pointer);
  110.  
  111.     void (* enum_add_constants)(PyObject *module, GType enum_type,
  112.                 const gchar *strip_prefix);
  113.     void (* flags_add_constants)(PyObject *module, GType flags_type,
  114.                  const gchar *strip_prefix);
  115.  
  116.     gchar *(* constant_strip_prefix)(gchar *name,
  117.                      const gchar *strip_prefix);
  118.  
  119.     gboolean (* error_check)(GError **error);
  120.  
  121.     /* hooks to register handlers for getting GDK threads to cooperate
  122.      * with python threading */
  123.     void (* set_thread_block_funcs) (PyGThreadBlockFunc block_threads_func,
  124.                      PyGThreadBlockFunc unblock_threads_func);
  125.     PyGThreadBlockFunc block_threads;
  126.     PyGThreadBlockFunc unblock_threads;
  127.     PyTypeObject *paramspec_type;
  128.     PyObject *(* paramspec_new)(GParamSpec *spec);
  129.     GParamSpec *(*paramspec_get)(PyObject *tuple);
  130.     int (*pyobj_to_unichar_conv)(PyObject *pyobj, void* ptr);
  131.     gboolean (*parse_constructor_args)(GType        obj_type,
  132.                                        char       **arg_names,
  133.                                        char       **prop_names,
  134.                                        GParameter  *params,
  135.                                        guint       *nparams,
  136.                                        PyObject   **py_args);
  137.     PyObject *(* param_gvalue_as_pyobject) (const GValue* gvalue, 
  138.                                             gboolean copy_boxed,
  139.                         const GParamSpec* pspec);
  140.     int (* gvalue_from_param_pyobject) (GValue* value, 
  141.                                         PyObject* py_obj, 
  142.                     const GParamSpec* pspec);
  143.     PyTypeObject *enum_type;
  144.     PyObject *(*enum_add)(PyObject *module,
  145.               const char *typename_,
  146.               const char *strip_prefix,
  147.               GType gtype);
  148.     PyObject* (*enum_from_gtype)(GType gtype, int value);
  149.     
  150.     PyTypeObject *flags_type;
  151.     PyObject *(*flags_add)(PyObject *module,
  152.                const char *typename_,
  153.                const char *strip_prefix,
  154.                GType gtype);
  155.     PyObject* (*flags_from_gtype)(GType gtype, int value);
  156.  
  157.     gboolean threads_enabled;
  158.     int       (*enable_threads) (void);
  159.     int       (*gil_state_ensure) (void);
  160.     void      (*gil_state_release) (int flag);
  161.     void      (*register_class_init) (GType gtype, PyGClassInitFunc class_init);
  162.     void      (*register_interface_info) (GType gtype, const GInterfaceInfo *info);
  163. };
  164.  
  165. #ifndef _INSIDE_PYGOBJECT_
  166.  
  167. #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT)
  168. extern struct _PyGObject_Functions *_PyGObject_API;
  169. #else
  170. struct _PyGObject_Functions *_PyGObject_API;
  171. #endif
  172.  
  173. #define pygobject_register_class    (_PyGObject_API->register_class)
  174. #define pygobject_register_wrapper  (_PyGObject_API->register_wrapper)
  175. #define pygobject_register_sinkfunc (_PyGObject_API->register_sinkfunc)
  176. #define pygobject_lookup_class      (_PyGObject_API->lookup_class)
  177. #define pygobject_new               (_PyGObject_API->newgobj)
  178. #define pyg_closure_new             (_PyGObject_API->closure_new)
  179. #define pygobject_watch_closure     (_PyGObject_API->object_watch_closure)
  180. #define pyg_destroy_notify          (_PyGObject_API->destroy_notify)
  181. #define pyg_type_from_object        (_PyGObject_API->type_from_object)
  182. #define pyg_type_wrapper_new        (_PyGObject_API->type_wrapper_new)
  183. #define pyg_enum_get_value          (_PyGObject_API->enum_get_value)
  184. #define pyg_flags_get_value         (_PyGObject_API->flags_get_value)
  185. #define pyg_register_boxed_custom   (_PyGObject_API->register_boxed_custom)
  186. #define pyg_value_from_pyobject     (_PyGObject_API->value_from_pyobject)
  187. #define pyg_value_as_pyobject       (_PyGObject_API->value_as_pyobject)
  188. #define pyg_register_interface      (_PyGObject_API->register_interface)
  189. #define PyGBoxed_Type               (*_PyGObject_API->boxed_type)
  190. #define pyg_register_boxed          (_PyGObject_API->register_boxed)
  191. #define pyg_boxed_new               (_PyGObject_API->boxed_new)
  192. #define PyGPointer_Type             (*_PyGObject_API->pointer_type)
  193. #define pyg_register_pointer        (_PyGObject_API->register_pointer)
  194. #define pyg_pointer_new             (_PyGObject_API->pointer_new)
  195. #define pyg_enum_add_constants      (_PyGObject_API->enum_add_constants)
  196. #define pyg_flags_add_constants     (_PyGObject_API->flags_add_constants)
  197. #define pyg_constant_strip_prefix   (_PyGObject_API->constant_strip_prefix)
  198. #define pyg_error_check             (_PyGObject_API->error_check)
  199. #define pyg_set_thread_block_funcs  (_PyGObject_API->set_thread_block_funcs)
  200. #define PyGParamSpec_Type           (*_PyGObject_API->paramspec_type)
  201. #define pyg_param_spec_new          (_PyGObject_API->paramspec_new)
  202. #define pyg_param_spec_from_object  (_PyGObject_API->paramspec_get)
  203. #define pyg_pyobj_to_unichar_conv   (_PyGObject_API->pyobj_to_unichar_conv)
  204. #define pyg_parse_constructor_args  (_PyGObject_API->parse_constructor_args)
  205. #define pyg_param_gvalue_from_pyobject (_PyGObject_API->value_from_pyobject)
  206. #define pyg_param_gvalue_as_pyobject   (_PyGObject_API->value_as_pyobject)
  207. #define PyGEnum_Type                (*_PyGObject_API->enum_type)
  208. #define pyg_enum_add                (_PyGObject_API->enum_add)
  209. #define pyg_enum_from_gtype         (_PyGObject_API->enum_from_gtype)
  210. #define PyGFlags_Type               (*_PyGObject_API->flags_type)
  211. #define pyg_flags_add               (_PyGObject_API->flags_add)
  212. #define pyg_flags_from_gtype        (_PyGObject_API->flags_from_gtype)
  213. #define pyg_enable_threads          (_PyGObject_API->enable_threads)
  214. #define pyg_register_class_init     (_PyGObject_API->register_class_init)
  215. #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
  216.  
  217. #define pyg_block_threads()   G_STMT_START {   \
  218.     if (_PyGObject_API->block_threads != NULL) \
  219.       (* _PyGObject_API->block_threads)();     \
  220.   } G_STMT_END
  221. #define pyg_unblock_threads() G_STMT_START {     \
  222.     if (_PyGObject_API->unblock_threads != NULL) \
  223.       (* _PyGObject_API->unblock_threads)();     \
  224.   } G_STMT_END
  225.  
  226. #define pyg_threads_enabled (_PyGObject_API->threads_enabled)
  227.  
  228. #define pyg_gil_state_ensure() (_PyGObject_API->threads_enabled? (_PyGObject_API->gil_state_ensure()) : 0)
  229. #define pyg_gil_state_release(state) G_STMT_START {     \
  230.     if (_PyGObject_API->threads_enabled)                \
  231.         _PyGObject_API->gil_state_release(state);       \
  232.     } G_STMT_END
  233.  
  234. #define pyg_begin_allow_threads                 \
  235.     G_STMT_START {                              \
  236.         PyThreadState *_save = NULL;            \
  237.         if (_PyGObject_API->threads_enabled)    \
  238.             _save = PyEval_SaveThread();
  239. #define pyg_end_allow_threads                   \
  240.         if (_PyGObject_API->threads_enabled)    \
  241.             PyEval_RestoreThread(_save);        \
  242.     } G_STMT_END
  243.  
  244. #define init_pygobject() { \
  245.     PyObject *gobject = PyImport_ImportModule("gobject"); \
  246.     if (gobject != NULL) { \
  247.         PyObject *mdict = PyModule_GetDict(gobject); \
  248.         PyObject *cobject = PyDict_GetItemString(mdict, "_PyGObject_API"); \
  249.         if (PyCObject_Check(cobject)) \
  250.             _PyGObject_API = (struct _PyGObject_Functions *)PyCObject_AsVoidPtr(cobject); \
  251.         else { \
  252.             PyErr_SetString(PyExc_RuntimeError, \
  253.                             "could not find _PyGObject_API object"); \
  254.         return; \
  255.         } \
  256.     } else { \
  257.         PyErr_SetString(PyExc_ImportError, \
  258.                         "could not import gobject"); \
  259.         return; \
  260.     } \
  261. }
  262.  
  263. #endif /* !_INSIDE_PYGOBJECT_ */
  264.  
  265. G_END_DECLS
  266.  
  267. #endif /* !_PYGOBJECT_H_ */
  268.